<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          v-hasPermi="['purchase:pfmCheck:query']"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleCheck"
          v-hasPermi="['purchase:pfmCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleBack"
          v-hasPermi="['purchase:pfmCheck:back']"
        >
          撤销
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:pfmCheck:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid green;
              background-color: green;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">未审核</span>
        </div>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid #bb0331;
              background-color: #bb0331;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">已审核</span>
        </div>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="720px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="采购单号">
            <el-input
              v-model="queryParams.pomDjbh"
              placeholder="请输入采购单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合查询">
            <el-input
              v-model="queryParams.remark"
              clearable
              placeholder="请输入品名规格或客户编码"
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangePomJlrq"
              size="small"
              :clearable="false"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单据状态">
            <el-radio-group v-model="queryParams.pomZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 头表Table -->
    <el-table
      highlight-current-row
      v-loading="loading"
      :data="pomMstrList"
      @row-click="clickPodDet"
      :cell-style="iCellStyle"
      border
      stripe
      height="calc(100% - 100px)"
      :height="tableH"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="采购单号"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomDjbh"
      />
      <el-table-column
        label="入库类型"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomType"
      >
        <template #default="scope">
          <dict-tag :options="psTypeOptions" :value="scope.row.pomType" />
        </template>
      </el-table-column>
      <el-table-column
        label="入库仓库"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomCkdm"
      >
        <template #default="scope">
          <dict-tag :options="storageOptions" :value="scope.row.pomCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomJhrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomBackdesc"
      />
      <el-table-column
        label="单据状态"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomZtbz"
      >
        <template #default="scope">
          <dict-tag :options="ZtbzOptionss" :value="scope.row.pomZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomJlrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomShrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 详细表Table -->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="采购明细" name="first">
        <el-table
          v-loading="loading"
          :data="podDetList"
          :height="tableHChild"
          stripe
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="#" width="40" align="center" />
          <el-table-column label="请购单号" align="center" prop="podQgdh" />
          <el-table-column label="销售单号" align="center" prop="podSodh" />
          <el-table-column label="物料代码" align="center" prop="podWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="采购属性" align="center" prop="podCgsx">
            <template #default="scope">
              <dict-tag :options="CgsxOptions" :value="scope.row.podCgsx" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="itmMinbz" />
          <el-table-column label="采购数量" align="center" prop="podCgsl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="变更原因" align="center" prop="podBgdesc" />
          <el-table-column label="变更人员" align="center" prop="podBgry" />
          <el-table-column
            label="变更日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="podBgrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.podBgrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="建立人员" align="center" prop="podJlry" />
          <el-table-column
            label="建立日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="podJlrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.podJlrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="120"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="handPodBg(scope.row)"
                v-hasPermi="['purchase:pomcheck:bg']"
                >变更</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="采购统计" name="second">
        <el-table
          v-loading="loading"
          :data="podDetCountList"
          :height="tableHChild"
          stripe
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="#" width="40" align="center" />
          <el-table-column label="物料代码" align="center" prop="podWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="采购属性" align="center" prop="podCgsx">
            <template #default="scope">
              <dict-tag :options="CgsxOptions" :value="scope.row.podCgsx" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="itmMinbz" />
          <el-table-column label="采购数量" align="center" prop="podCgsl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!--撤回输入原因-->
    <el-dialog
      title="确认撤销?"
      v-model="backOpen"
      :close-on-click-modal="false"
      draggable
      width="350px"
      append-to-body
      center
    >
      <el-input
        style="margin-top: -10px; width: 200px; left: 18%"
        placeholder="请输入撤回原因"
        v-model="backReson"
        clearable
      />
      <el-button
        style="margin-top: 20px; width: 300px"
        type="primary"
        @click="handlebackReson"
        >确认</el-button
      >
    </el-dialog>
    <!--变更-->
    <el-dialog
      title="变更采购数量"
      v-model="BgOpen"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="BgForm" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="采购单号" prop="pomDjbh">
            <el-input v-model="BgForm.podDjbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料代码" prop="podWldm">
            <el-input v-model="BgForm.podWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="itmPmgg">
            <el-input v-model="BgForm.itmPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购数量" prop="podCgsl">
            <el-input v-model="BgForm.podCgsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最小包装" prop="itmMinbz">
            <el-input v-model="BgForm.itmMinbz" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="变更数量" prop="podBgsl">
            <el-input v-model="BgForm.podBgsl" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="变更原因" prop="podBgdesc">
            <el-input type="textarea" v-model="BgForm.podBgdesc" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBg">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPomMstr,
  backResonPomMstr,
  exportPomMstr,
  changePomMstr,
} from "@/api/purchase/pomMstr";
import { getPstorageType } from "@/api/repertory/putstorage";
import { getstorageType } from "@/api/repertory/Storage";
import { getAllCgList } from "@/api/project/mstr";
import { listsPodDet } from "@/api/purchase/podDet";
import { updatePodDet } from "../../../../api/purchase/podDet";

export default {
  name: "PomCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      //默认子表
      activeTab: "first",
      // 详细表List
      podDetList: [],
      podDetCountList: [],
      //采购单号
      podDjbhc: null,
      pomCkdmc: null,
      // 导出遮罩层
      tableH:
        document.body.clientHeight > 900
          ? document.body.clientHeight / 2 - 96
          : document.body.clientHeight / 2 - 95,
      tableHChild: document.body.clientHeight / 2 - 150,
      exportLoading: false,
      // 选中数组
      ids: [],
      pomDjbhs: [],
      clickPomMstr: [],
      // 非单个禁用
      single: true,
      //撤回理由
      backReson: "",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 生产采购表格数据
      pomMstrList: [],
      //撤回模态
      backOpen: false,
      //变更模态
      BgOpen: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangePomJlrq: [],
      // 入库类型字典
      psTypeOptions: [],
      // 入库仓库字典
      storageOptions: [],
      //采购属性字典
      CgsxOptions: [],
      // 单位字典
      unitFlagOptions: [],
      // 状态标志字典
      ZtbzOptions: [],
      ZtbzOptionss: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 15 : 10,
        pageType: 2,
        pomTjflag: 1,
        pomDjbh: null,
        pomZtbz: "1",
        pomJlrq: null,
        pomType: null,
      },
      // 表单参数
      form: {},
      // 变更表单参数
      BgForm: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    //入库类型字典
    getPstorageType(0).then((response) => {
      this.psTypeOptions = response.data;
      for (let i of this.psTypeOptions) {
        i.listClass = "default";
      }
    });
    this.getDicts("sys_purchase_state").then((response) => {
      for (let i of response.data) {
        this.ZtbzOptionss.push(i);
      }
      this.ZtbzOptions = response.data;
      const flag = { dictLabel: "新增", dictValue: "0", listClass: "default" };
      this.ZtbzOptionss.push(flag);
    });
    //入库仓库字典
    getstorageType(0).then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.storageName;
        i.dictValue = i.storageId + "";
        i.listClass = "default";
      }
      this.storageOptions = response.data;
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //采购属性
    getAllCgList().then((response) => {
      this.CgsxOptions = response.data;
      for (let i of this.CgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
  },
  methods: {
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询生产采购列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePomJlrq && "" != this.daterangePomJlrq) {
        this.queryParams.params["beginPomJlrq"] = this.daterangePomJlrq[0];
        this.queryParams.params["endPomJlrq"] = this.daterangePomJlrq[1];
      }
      listPomMstr(this.queryParams).then((response) => {
        this.podDetList = [];
        this.podDetCountList = [];
        this.pomMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickPodDet(row);
        } else if (this.pomMstrList != null && this.pomMstrList.length > 0) {
          this.clickPodDet(this.pomMstrList[0]);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.BgOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pomId: null,
        pomDjbh: null,
        pomJhrq: new Date(),
        pomZtbz: null,
        pomDesc: null,
        pomBackdesc: null,
        pomJlry: null,
        pomJlrq: null,
        pomShry: null,
        pomShrq: null,
        pomTjflag: null,
        pomCkdm: null,
        pomType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePomJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      if (row.pomZtbz === 1) {
        return "padding:0px 0;color:green;";
      }
      //已审核
      else if (row.pomZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.pomId);
      this.clickPomMstr = selection;
      this.pomDjbhs = selection.map((item) => item.pomDjbh);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产采购";
      this.openSearch = true;
    },
    /** 审核操作按钮*/
    handleCheck() {
      if (this.clickPomMstr[0].pomZtbz != 1) {
        this.msgError(
          "采购订单: " +
            this.clickPomMstr[0].pomDjbh +
            " 不是未审核状态,无法审核~"
        );
        return;
      }
      const podDjbh = this.pomDjbhs;
      changePomMstr(podDjbh, 2, null).then((response) => {
        this.msgSuccess("审核成功~");
        this.getList();
      });
    },
    /** 撤回操作按钮*/
    handleBack() {
      if (this.clickPomMstr[0].pomZtbz != 1) {
        this.msgError(
          "采购订单: " +
            this.clickPomMstr[0].pomDjbh +
            " 不是未审核状态,无法撤回~"
        );
        return;
      }
      this.backOpen = true;
    },
    handlebackReson() {
      const podDjbh = this.pomDjbhs;
      changePomMstr(podDjbh, 1, this.backReson).then((response) => {
        this.backOpen = false;
        this.getList();
        this.msgSuccess("撤回成功~");
      });
    },
    //变更
    handPodBg(row) {
      this.BgForm = row;
      this.BgOpen = true;
    },
    //变更确认
    handleBg() {
      this.BgForm.podBgry = this.$store.state.user.name;
      console.log(this.$store.state);
      this.BgForm.podCgsl = this.BgForm.podBgsl;
      this.BgForm.podBgrq = new Date();
      updatePodDet(this.BgForm).then((response) => {
        this.msgSuccess("变更成功!");
        this.BgOpen = false;
        this.getList();
      });
    },
    /** 查看详细表数据 */
    clickPodDet(row) {
      // this.childrenDisabled = row.pomZtbz !=1
      const data = { podDjbh: row.pomDjbh };
      listsPodDet(data).then((response) => {
        this.podDetList = response.data.detail;
        this.podDetCountList = response.data.total;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产采购数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportPomMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style type="text/css">
input::-ms-input-placeholder {
  text-align: center;
}
input::-webkit-input-placeholder {
  text-align: center;
}
</style>
